//
//  HomeController.swift
//  CarShare
//
//  Created by Pro on 2017/12/13.
//

import UIKit

class HomeController: BaseController ,MAMapViewDelegate ,ButtonEventDelegete{
    
    
    func buttonBeTouched(_ sender: SpecialAnnotationView!) {
        print(sender.needIndex)
    }
    
    
    @IBOutlet weak var MapBgView: UIView!
    
    var annotations: Array<SpecialPointAnnotation>!

    var mapView: MAMapView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.groupTableViewBackground
        
        initMapView()
        addStopsAnnotions()


    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        mapView.showsUserLocation = true
        mapView.userTrackingMode = MAUserTrackingMode.follow
        
        
        mapView.addAnnotations(annotations)
        mapView.showAnnotations(annotations, edgePadding: UIEdgeInsetsMake(20, 20, 20, 20), animated: true)
        mapView.selectAnnotation(annotations.first, animated: true)
    }
    
    
    func addStopsAnnotions() -> Void {
        

        annotations = Array()

        let coordinates: [CLLocationCoordinate2D] = [
            CLLocationCoordinate2D(latitude: 39.992520, longitude: 116.336170),
            CLLocationCoordinate2D(latitude: 39.978234, longitude: 116.352343),
            CLLocationCoordinate2D(latitude: 39.998293, longitude: 116.348904),
            CLLocationCoordinate2D(latitude: 40.004087, longitude: 116.353915),
            CLLocationCoordinate2D(latitude: 40.001442, longitude: 116.353915),
            CLLocationCoordinate2D(latitude: 39.989105, longitude: 116.360200),
            CLLocationCoordinate2D(latitude: 39.989098, longitude: 116.360201),
            CLLocationCoordinate2D(latitude: 39.998439, longitude: 116.324219),
            CLLocationCoordinate2D(latitude: 39.979590, longitude: 116.352792)]

        for (idx, coor) in coordinates.enumerated() {
            let anno = SpecialPointAnnotation()
            anno.coordinate = coor
            anno.title = String(idx)
            anno.subtitle = String(9)
            annotations.append(anno)
        }

        
    }
    
    
    func initMapView() {
        
        mapView = MAMapView(frame: self.MapBgView.bounds)
        mapView.delegate = self
        self.MapBgView.addSubview(mapView!)
    }
    
    func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
        
        if annotation.isKind(of: SpecialPointAnnotation.self) {
            let pointReuseIndetifier = "SpecialAnnotationViewidentifier"
            var annotationView: SpecialAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! SpecialAnnotationView?
            
            if annotationView == nil {
                annotationView = SpecialAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier)
            }
            annotationView?.delegate = self
            annotationView!.canShowCallout = true
//            annotationView!.animatesDrop = true
            annotationView!.isDraggable = true
            annotationView?.canShowCallout = false
//            annotationView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.detailDisclosure)
//            let idx = annotations.index(of: annotation as! MAPointAnnotation)
//            annotationView!.pinColor = MAPinAnnotationColor(rawValue: idx!)!
            
            return annotationView!
        }
        
        return nil
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    

    

    @IBAction func PersonalInfoAction(_ sender: UIButton) {
        navigationController?.pushViewController(PersonalController(), animated: false)
    }
    
    @IBAction func MessageInfoAction(_ sender: UIButton) {
        navigationController?.pushViewController(MessageController(), animated: false)
    }
    
}
